package com.wanlian.service.impl;

import java.util.List;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.BannerDao;
import com.wanlian.entity.Banner;
import com.wanlian.error.ErrorCode;
import com.wanlian.error.SuccessCode;
import com.wanlian.model.ResultModel;
import com.wanlian.service.BannerServices;
import com.wanlian.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BannerServicesImpl implements BannerServices {

	@Autowired
	private BannerDao bannerDao;

	@Override
	public ResultModel doAdd(Banner banner) {
		bannerDao.add(banner);
		ResultModel resultModel = new ResultModel();
		resultModel.setBresult(true);
		resultModel.setMessage(SuccessCode.GROUP_ADD);
		return resultModel;
	}

	@Override
	public void doedit(Banner banner) {
		bannerDao.doedit(banner);
	}

	@Override
	public Banner queryBanner(int id) {
		return bannerDao.queryBanner(id);
	}


	@Override
	public List<Banner> queryBannerList(Page page) {
		return bannerDao.queryBannerList(page);
	}

	@Override
	public List<Banner> queryAllForApp() {
		return bannerDao.queryAllForApp();
	}

	@Override
	public ResultModel queryBannersList(PageCondition pageCondition) {
		return bannerDao.queryBannersList(pageCondition);
	}
	
	@Override
	public ResultModel doSetIsTop(int id) {
		ResultModel resultModel=new ResultModel();
		Banner banner = bannerDao.queryBanner(id);
		if(banner!=null){
			if(!banner.isEnable()){
				resultModel.setBresult(false);
				resultModel.setMessage(ErrorCode.BANNER_TOP_ERO);
			}else{
				boolean b = this.bannerDao.setIsTop(id);
				resultModel.setBresult(b);
				if(b){
					resultModel.setMessage(SuccessCode.BANNER_TOP_SUC);
				}else{
					resultModel.setMessage(ErrorCode.BANNER_TOP_DEF);
				}
			}

		}else{
			resultModel.setBresult(false);
			resultModel.setMessage(ErrorCode.BANNER_NOT_EXIST);
		}
		return resultModel;
	}

	@Override
	public ResultModel doSetEnable(int id) {
		ResultModel resultModel=new ResultModel();
		if(bannerDao.setEnable(id)) {
			resultModel.setBresult(true);
			resultModel.setMessage(SuccessCode.BANNER_ENABLE_SUC);
		}else{
			resultModel.setBresult(false);
			resultModel.setMessage(ErrorCode.BANNER_ENABLE_DEF);
		}
		return resultModel;
	}

	@Override
	public ResultModel doSetDisable(int id) {
		ResultModel resultModel=new ResultModel();
		if(bannerDao.setDisable(id)) {
			resultModel.setBresult(true);
			resultModel.setMessage(SuccessCode.BANNER_DISABLE_SUC);
		}else{
			resultModel.setBresult(false);
			resultModel.setMessage(ErrorCode.BANNER_DISABLE_DEF);
		}
		return resultModel;
	}

	@Override
	public ResultModel doSetNotTop(int id) {
		ResultModel resultModel = new ResultModel();
		Banner banner = bannerDao.queryBanner(id);
		if(banner.isTop()){
			bannerDao.setNotTop(id);
			resultModel.setBresult(true);
			resultModel.setMessage(SuccessCode.BANNER_TOP_DIS);
		}else{
			resultModel.setBresult(false);
			resultModel.setMessage(ErrorCode.BANNER_NOT_TOP);
		}
		return resultModel;
	}

	@Override
	public ResultModel doDelete(int id) {
		ResultModel resultModel = new ResultModel();
		bannerDao.doDelete(id);
		resultModel.setBresult(true);
		resultModel.setMessage(SuccessCode.STORAGE_DELETE);
		return resultModel;
	}

	@Override
	public ResultModel doUpdate(Banner banner) {
		ResultModel resultModel = new ResultModel();
		Banner dbBanner = bannerDao.queryBanner(banner.getId());
		if(banner.getAttribute1()!=null&&banner.getAttribute1()!=""){
			if(!dbBanner.getAttribute1().equals(banner.getAttribute1())){
				dbBanner.setAttribute1(banner.getAttribute1());
			}
		}
		if(banner.getImgPath()!=null&&banner.getImgPath()!=""){
			if(!dbBanner.getImgPath().equals(banner.getImgPath())){
				dbBanner.setImgPath(banner.getImgPath());
			}
		}
		if(banner.getTitle()!=null&&banner.getTitle()!=""){
			if(!dbBanner.getTitle().equals(banner.getTitle())){
				dbBanner.setTitle(banner.getTitle());
			}
		}
		bannerDao.doedit(dbBanner);
		resultModel.setBresult(true);
		resultModel.setMessage(SuccessCode.SUCCESS_UPD);
		return resultModel;
	}
}
